﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataAccessLayer;
using MyCommon;

namespace BussinessLayer
{
    public class NguoiDungBO : BaseBO
    {
        #region Singleton implemented
        private static NguoiDungBO _instance;
        private NguoiDungBO()
        { }
        public static NguoiDungBO getInstance
        {
            get
            {
                if (_instance == null)
                {
                    _instance = new NguoiDungBO();
                }
                return _instance;
            }
        }
        #endregion

        public IEnumerable<NguoiDung> GetAllNguoiDung()
        {
            return _dao.NguoiDung.GetAllNguoiDung();
        }
        public IEnumerable<NguoiDung> GetAllNguoiDungChuaSuDung()
        {
            return _dao.NguoiDung.GetAllNguoiDungChuaSuDung();
        }
        public NguoiDung GetNguoiDungById(int id)
        {
            return _dao.NguoiDung.GetNguoiDungById(id);
        }
        public bool UpdateNguoiDung(NguoiDung inNguoiDung)
        {
            return _dao.NguoiDung.UpdateNguoiDung(inNguoiDung);
        }
        public bool InsertNguoiDung(NguoiDung inNguoiDung)
        {
            return _dao.NguoiDung.InsertNguoiDung(inNguoiDung);
        }
        public ReturnValue<bool> DelNguoiDung(int id)
        {
            return _dao.NguoiDung.DelNguoiDung(id);
        }
        //public string GenerateMaNguoiDung()
        //{
        //    NguoiDung aObj = _dao.NguoiDung.GetLastRecord();
        //    if (aObj != null)
        //    {
        //        return Utils.GenerateCode(CodeType.NguoiDung, aObj.Id + 1);
        //    }
        //    else
        //    {
        //        return Utils.GenerateCode(CodeType.NguoiDung, 1);
        //    }
        //}
        public NguoiDung GetLastRecord()
        {
            return _dao.NguoiDung.GetLastRecord();
        }

        public ReturnValue<bool> Login(string inUsername, string inPwd)
        {
            ReturnValue<bool> ret = new ReturnValue<bool>(true, "");
            if (!_dao.NguoiDung.IsUsernameExist(inUsername))
            {
                ret.RetValue = false;
                ret.Msg = "Tên đăng nhập không tồn tại";
            }
            else if (!_dao.NguoiDung.IsPwdMatch(inUsername, inPwd))
            {
                ret.RetValue = false;
                ret.Msg = "Mật khẩu không đúng";
            }
            else if (!_dao.NguoiDung.IsActiveAccount(inUsername))
            {
                ret.RetValue = false;
                ret.Msg = "Tài khoản đang bị khóa";
            }
            return ret;
        }

        public ReturnValue<bool> ChangePwd(string inUsername, string inOldPwd, string inNewPwd)
        {
            ReturnValue<bool> ret = new ReturnValue<bool>(true, "");
            if (!_dao.NguoiDung.IsPwdMatch(inUsername, inOldPwd))
            {
                ret.RetValue = false;
                ret.Msg = "Mật khẩu cũ không đúng";
            }
            else
            {
                NguoiDung obj = _dao.NguoiDung.GetNguoiDungByUsername(inUsername);
                obj.Password = inNewPwd;
                _dao.NguoiDung.UpdateNguoiDung(obj);
            }
            return ret;
        }

        public NguoiDung GetNguoiDungByUsername(string inUsername)
        {
            return _dao.NguoiDung.GetNguoiDungByUsername(inUsername);
        }

        public IEnumerable<DataAccessLayer.VaiTro> GetAllVaiTro()
        {
            return _dao.NguoiDung.GetAllVaiTro();
        }

    }
}
